package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysRoleService;
import com.atguigu.spzx.model.dto.system.SysRoleDto;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
//@CrossOrigin(allowCredentials = "true",originPatterns = "*",allowedHeaders = "*")
public class SysRoleController {

    @Autowired
    SysRoleService sysRoleService;


    @Operation(summary = "分页")
    @PostMapping("/findByPage/{pageNum}/{pageSize}")
    public Result<PageInfo<SysRole>> findByPage(@PathVariable("pageNum") Integer pageNum,
                             @PathVariable("pageSize") Integer pageSize,
                             @RequestBody SysRoleDto sysRoleDto) {
        PageInfo<SysRole> pageInfo =  sysRoleService.findByPage(pageNum,pageSize,sysRoleDto);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "添加")
    @PostMapping("/save")
    public Result save(@RequestBody SysRole sysRole){
        sysRoleService.save(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "根据ID获取数据")
    @GetMapping("/getById/{id}")
    public Result<SysRole> getById(@PathVariable Long id){
        SysRole sysRole = sysRoleService.getById(id);
        return Result.build(sysRole,ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "修改")
    @PutMapping("/update")
    public Result update(@RequestBody SysRole sysRole){
        sysRoleService.update(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "根据ID删除数据(逻辑删除)")
    @DeleteMapping("/deleteById/{id}")
    public Result<SysRole> deleteById(@PathVariable Long id){
        sysRoleService.deleteById(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "分配角色，查询所有角色以及用户拥有的角色id集合")
    @GetMapping("/findAllRoles/{id}")
    public Result<Map<String, List>> findAllRoles(@PathVariable("id") Long userId){
        Map<String, List> data = sysRoleService.findAllRoles(userId);
        return Result.build(data,ResultCodeEnum.SUCCESS);
    }









}
